subroutine read_ML_residuals_csv_2(c,nd,stanum,evnum,nsta,nev)
USE IFPORT 
USE PARSE_CSV
    include 'seisev_EVENT_PHASE_structure.FI'
    include 'inversione_pesata_multipar.FI'
    character*80 fil
    character*1024 line
    integer stanum(150000),evnum(150000)
    logical success
    integer istart,iend,len,ich,i1,i2,add,i,ier,nd,nsta,nev
    character*1 sep
    data sep/','/

    write(*,*)' subroutine: read_ML_residuals_csv'
    is=0
! open catalog as defined manually after various analysis
    success=systemqq(' dir  ')
    1 write(*,*)' fornire il nome del file con la selezione di ampiezze di stazione '
    read(*,'(a)',err=1)fil
    
! file di tipo fil='A_all_modified2.csv'   !!!
    open(status='old',readonly,unit=2,file=fil,err=99)

    n=0
    nd=0
    2 read(2,'(q,a)',end=9)len,line
    
! count the read records
    n=n+1  
    i=mod(n,1000)
    if(i.eq.0 .and. nd.gt.0)write(*,*)' n ',n,' nd ',nd
    if(line(1:1).eq.'#')then
! it is only a comment
       goto 2
    else    
!  READ AN HYPOCENTER
       nd=nd+1
       i1=1

!#sta,cha,ot,Hy-dist,amp1,amp2,WA_pha_amp,MLDB_dif,MLDB_sta,
!MLDB_s_wgt,MLDB_ev,MLDB_sd,lat,lon,depth,mag,N_WA_DB,NEV,
!N_WA_P_EV,N_WA_P_STA,sta.number, ev.number

       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).sta   ,ich )
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).cha   ,ich )  
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).ot    ,ich ) 
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).s_h_hypdist)
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp1)
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).amp2)
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).WA_pha_amp)
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).MLDB_dif)
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).MLDB_sta)
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).MLDB_s_wgt)
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).MLDB_ev)
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).MLDB_sd)
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).lat)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).lon)    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).depth  )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).mag  )    
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).N_WA_DB)
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).NEV)
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).N_WA_P_EV)
       call parse_csv_var(sep,ier,line,i1,i2,len, c(nd).N_WA_P_STA)
       call parse_csv_var(sep,ier,line,i1,i2,len, stanum(nd))
       call parse_csv_var(sep,ier,line,i1,i2,len, evnum(nd))
       
       
!         write(*,'(a5,a1,a3,a1,a19,a1,4(f15.5,a1),9(f7.3,a1),6(i6,a1))')     &
!         c(nd).sta,',',c(nd).cha,',',c(nd).ot,',',c(nd).s_h_hypdist,',',             &
!         c(nd).amp1,',',c(nd).amp2,',',c(nd).wa_pha_amp,',',c(nd).MLDB_dif,',',  &
!         c(nd).MLDB_sta,',',c(nd).MLDB_s_wgt,',',c(nd).MLDB_ev,',',c(nd).MLDB_sd,',', &
!         c(nd).lat,',',c(nd).lon,',',c(nd).depth,',',c(nd).mag,',',c(nd).N_WA_DB,    &
!         ',',c(nd).NEV,',',c(nd).N_WA_P_EV,',',c(nd).N_WA_P_STA,                &
!         ',',stanum(nd),',',evnum(nd)
      
    endif
  
    goto 2

9   write(*,*)' I have read ',n,' records'
    write(*,*)' and ',nd,' good phases'
    
    ! trovo quanti eventi e quante stazioni stiamo invertendo.
    nsta=0
    nev=0
    do i=1,nd
      if(stanum(i) .gt. nsta)nsta=stanum(i)
      if(evnum(i) .gt. nev)nev=evnum(i)
    enddo
    !pause!pause
    return

99  write(*,*)' error in reading catalog at record ',n
    pause 
    stop
return
end
! **************************************************
